<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body style="padding-top: 100px" class="layui-bg-black">
<!-- 你的 HTML 代码 start -->

<div class="layui-container ">
    <div class="layui-row">

        <div class="layui-col-md4 layui-col-md-offset4">


            <div class="layui-card layui-bg-gray">
                <div class="layui-card-header">请登录</div>
                <div class="layui-card-body">
                    <form class="layui-form">

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i> </label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" id="username" required lay-verify="required"
                                       placeholder="请输入用户名"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-password"></i> </label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" id="password" required lay-verify="required"
                                       placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-vercode"></i> </label>
                            <div class="layui-input-inline">
                                <input type="text" name="captcha" id="cap" required lay-verify="required"
                                       placeholder="请输入验证码"
                                       autocomplete="off" class="layui-input">
                            </div>

                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-vercode"></i> </label>
                            <div class="layui-input-inline">
                                <img id="captchaImg" src="/kaptcha/render" style="width: 185px">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">免登录</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="switch1" lay-skin="switch">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="loginForm">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>


        </div>

    </div>
</div>


<!-- 你的 HTML 代码 end -->

<script>
    $(function () {
        $("#captchaImg").click(function () {
            //让图片重新发送请求给验证码servlet
            $("#captchaImg").attr('src', '/kaptcha/render?t=' + new Date().getTime());
        });

    });
</script>
<script src="./layui/layui.js"></script>
<script>
    layui.use(function () {
        var layer = layui.layer
            , form = layui.form;

        var $ = layui.$;  // 引入jquery的核心对象

        //监听提交
        form.on('submit(loginForm)', function (data) {

            console.log(data.field);// 把表单中填好的数据 对应 的js对象 打印出来

            // layer.msg(JSON.stringify(data.field));

            // 发送ajax请求给后端登录接口

            $.ajax({
                url: '/kaptcha/valid',
                type: 'post',
                data: data.field,
                success: function (vo) {
                    console.log(vo);
                    if (vo.code == 1) {
                        window.location.href = "/plateform.html";
                    } else {
                        layer.msg(vo.msg);
                    }
                }
            });


            // 取消表单的默认提交行为
            return false;
        });

    });


    // $(function () {
    //
    // });
</script>
</body>
</html>
